import Vue from "vue";
import pageing from "../../../components/pageing/pageing.js";
import {request} from "../../../util/request.js";
import dialog from "../../../util/dialog";
import mediaView from "../../../components/mediaView/mediaView";
import "../../../style/basic.less";
import "./check.less";
new Vue({
    el:"#louie",
    data:{
        returnNum:10,  //请求数据数量
        pageNum:1,  //当前page页面
        total:0,
        list:[],   //列表
        media:"",  //查看media的url
        media_show:false,
        media_type:"image",
        select_all:false,   //是否全选
    },
    components:{
        page:pageing,
        "media-view":mediaView
    },
    created:function(){
        this.getTotal()
            .then(()=>{
                if(this.total > 0){
                    this.getList();
                }else{
                    this.list = [];
                }
            });
    },
    methods:{
        /**
         * 获取列表数据总数
         */
        getTotal(){
            return new Promise((success)=>{
                request('mediaCheck/getTotal')
                    .then((res)=>{
                        if(res.data.status === "success"){
                            this.total = parseInt(res.data.data);
                            success();
                        }
                    });
            });
        },
        /**
         * 获取列表
         */
        getList(){
            request("mediaCheck/getList",{
                limit:[(this.pageNum-1)*this.returnNum,this.returnNum]
            })
                .then((res)=>{
                    if(res.data.status === "success"){
                        this.list = res.data.data;
                    }
                });
        },
        /**
         * 点击分页
         * @param page
         */
        changePage:function(page){
            this.pageNum = page;
            this.getList();
        },
        /**
         * 点击素材
         */
        clickMedia(type,media){
            this.media_type = type;
            this.media = media;
            this.media_show = true;
        },
        /**
         * 通过审核
         */
        pass(media_id){
            request("mediaCheck/passOne",{
                media_id
            })
                .then((res)=>{
                    if(res.data.status === "success"){
                        this.getTotal()
                            .then(()=>{
                                if(this.total > 0){
                                    this.getList();
                                }else{
                                    this.list = [];
                                }
                            });
                    }else{
                        dialog.alert(this,"操作失败");
                    }
                });
        },
        /**
         * 删除一个素材
         */
        del(media_id,type){
            request("mediaCheck/delOne",{
                media_id,
                type
            })
                .then((res)=>{
                    if(res.data.status === "success"){
                        this.getTotal()
                            .then(()=>{
                                if(this.total > 0){
                                    this.getList();
                                }else{
                                    this.list = [];
                                }
                            });
                    }else{
                        dialog.alert(this,"操作失败");
                    }
                });
        },
        /**
         * 全选
         */
        selectAll(){
            var batch = this.$refs.batchForm.batch;
            if(this.select_all){
                for(let i=0;i<batch.length;i++){
                    batch[i].checked = false;
                }
                this.select_all = false;
            }else{
                for(let i=0;i<batch.length;i++){
                    batch[i].checked = true;
                }
                this.select_all = true;
            }
        },
        /**
         * 批量操作
         */
        batchSubmit(res){
            var batch = this.$refs.batchForm.batch;
            let checked = [];
            for(let i=0;i<batch.length;i++){
                if(batch[i].checked){
                    let temp_item = batch[i].value.split('|')
                    checked.push({
                        media_id:temp_item[0],
                        media_type:temp_item[1]
                    });
                }
            }
            if(checked.length <= 0){
                dialog.alert(this,"最少选择一个素材");
                return false;
            }
            request("mediaCheck/batchHandle",{
                type:res,
                data:checked
            })
                .then((res)=>{
                    if(res.data.status === "success"){
                        this.getTotal()
                            .then(()=>{
                                if(this.total > 0){
                                    this.getList();
                                }else{
                                    this.list = [];
                                }
                            });
                    }
                });
        }
    }
});